package com.example.encrption;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

//非对称加密
//加密和解密两套密码
//为什么加密和解密不同密码
//飞马大定律：勾股定理3平方+4平方=5平方
public class RSAActivity extends AppCompatActivity {

    private PublicKey aPublic;
    private PrivateKey aPrivate;
    private TextView showKeys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rsa);
        initviwe();
    }

    private void initviwe() {
        showKeys= (TextView) findViewById(R.id.showKeys);
    }

    //生成公钥私钥
    public void generate(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //1获取公钥是撕咬的对象
                    KeyPairGenerator generator=KeyPairGenerator.getInstance("RSA");
                      //2初始化,指定密码的长度bit
                    generator.initialize(1024);
                    //3获取公钥撕咬
                    KeyPair keyPair = generator.generateKeyPair();
                   //3.1获取私钥
                    //私保存好了，私钥丢了，找不回来了
                    aPrivate = keyPair.getPrivate();
                    //3.2获取私钥，弓腰丢了，可以通过撕咬推到
                    aPublic = keyPair.getPublic();
                    //撕咬的具体数据，撕咬字节数据，保存该数据，文件中，sp
                    //!!!必须进行Base64装换
                    byte[] aPrivateEncoded = aPrivate.getEncoded();

                    byte[] aPublicEncoded = aPublic.getEncoded();

                    //对公要和撕咬中的字节数组进行base64编码
                    final byte[] privatebase64 = Base64.encode(aPrivateEncoded, Base64.DEFAULT);

                    final byte[] publicBase64 = Base64.encode(aPublicEncoded, Base64.DEFAULT);
                    //显示到文本框中
                     showKeys.post(new Runnable() {
                         @Override
                         public void run() {
                              showKeys.setText("私钥是"+"\n"+new String(privatebase64)+"\n"+"公要是"+publicBase64);
                         }
                     });


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }


            }
        }).start();
    }

    //使用公要撕咬进行加密界面
    public void encrypt(View view) {
        //使用私钥取加密，使用公要进行解密

        String check="10000000000,123456";
        switch (view.getId()){
            case R.id.rsaEncrypt:
                //1获取加密引擎
                try {
                    Cipher cipher=Cipher.getInstance("RSA");

                     if (aPrivate!=null){
                         cipher.init(Cipher.ENCRYPT_MODE,aPrivate);
                     }
                    //3加密
                    byte[] bytes = cipher.doFinal(check.getBytes());
                    byte[] base64encode = Base64.encode(bytes, Base64.DEFAULT);
                    showKeys.setText("RSA加密的数据是"+new String(base64encode));


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }


                break;
            case R.id.rsaDncrypt:
                try {
                    Cipher cipher=Cipher.getInstance("RSA");

                    if (aPublic!=null){
                        cipher.init(Cipher.DECRYPT_MODE,aPublic);
                    }
                    byte[] bytes = cipher.doFinal(check.getBytes());
                    byte[] decode = Base64.decode(bytes, Base64.DEFAULT);
                     showKeys.setText("解密的结构是"+new String(decode));

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }

                break;
        }

    }
}
